//
//  DLShortInfoTC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2019/1/3.
//  Copyright © 2019 delongzhitou. All rights reserved.
//

#import "DLShortInfoTC.h"
#import "DLInfoListModel.h"

@interface DLShortInfoTC()


@property (nonatomic, strong) UILabel *emotionLabel;

@property (nonatomic, strong) UILabel *upLabel;

@property (nonatomic, strong) UILabel *upPercent;

@property (nonatomic, strong) UILabel *downPercent;

@property (nonatomic, strong) UILabel *downLabel;

@property (nonatomic, strong) UILabel *fromLabel;

@property (nonatomic, strong) UIButton *shareButton;

@property (nonatomic, strong) UILabel *bottomLabel;

@property (nonatomic, strong) DLShareView *shareView;

@property (nonatomic, assign) CGFloat percentWidth;

@end


@implementation DLShortInfoTC


- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutsubControls];
    }
    return self;
}
- (void) layoutsubControls {
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(20, 0, kScreenWidth - 40, 95);
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.textColor = kDLNormalBlackColor;
    titleLabel.font = kSystemFont(14);
    titleLabel.numberOfLines = 3;
    titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
//    titleLabel.backgroundColor = [UIColor cyanColor];
    
    UILabel *emotionLabel = [UILabel new];
    emotionLabel.frame = CGRectMake(titleLabel.left, titleLabel.bottom + 5, 70, 20);
    emotionLabel.text = @"情绪指数:";
    emotionLabel.textColor = kDLNormalBlackColor;
    emotionLabel.textAlignment = NSTextAlignmentLeft;
    emotionLabel.font = kSystemFont(14);
    [self.contentView addSubview:emotionLabel];
    self.emotionLabel = emotionLabel;
//    emotionLabel.backgroundColor = [UIColor greenColor];
    
    
    UILabel *upLabel = [UILabel new];
    upLabel.frame = CGRectMake(emotionLabel.right + 50, emotionLabel.top, 55, 20);
    upLabel.text = @"50%";
    upLabel.textAlignment = NSTextAlignmentCenter;
    upLabel.textColor = kColorRGB(255, 90, 62);
    upLabel.font = kSystemFont(14);
    [self.contentView addSubview:upLabel];
    self.upLabel = upLabel;
//    upLabel.backgroundColor = [UIColor purpleColor];
    
    
    UILabel *upPercent = [UILabel new];
    upPercent.frame = CGRectMake(upLabel.right, upLabel.top + 4, (titleLabel.width - emotionLabel.width - upLabel.width *2 - 50) / 2, upLabel.height - 8);
    upPercent.backgroundColor = kColorRGB(255, 90, 62);
    [self.contentView addSubview:upPercent];
    upPercent.layer.cornerRadius = 6;
    upPercent.clipsToBounds = YES;
    self.upPercent = upPercent;
    
    
    UILabel *downPercent = [UILabel new];
    downPercent.frame = CGRectMake(upPercent.right, upPercent.top, upPercent.width, upPercent.height);
    downPercent.backgroundColor = kColorRGB(126, 211, 33);
    [self.contentView addSubview:downPercent];
    downPercent.layer.cornerRadius = 6;
    downPercent.clipsToBounds = YES;
    self.downPercent = downPercent;
    
    
    UILabel *downLabel = [UILabel new];
    downLabel.frame = CGRectMake(downPercent.right, upLabel.top, upLabel.width, upLabel.height);
    downLabel.text = @"50%";
    downLabel.textAlignment = NSTextAlignmentCenter;
    downLabel.textColor = kColorRGB(126, 211, 33);
    downLabel.font = kSystemFont(14);
    [self.contentView addSubview:downLabel];
    self.downLabel = downLabel;;
//    downLabel.backgroundColor = [UIColor purpleColor];
    
    
    UILabel *fromLabel = [UILabel new];
    fromLabel.frame = CGRectMake(titleLabel.left, emotionLabel.bottom + 5, titleLabel.width - 30, 30);
    fromLabel.textAlignment = NSTextAlignmentLeft;
    fromLabel.textColor = kColorRGB(140, 140, 140);
    fromLabel.font = kSystemFont(14);
    [self.contentView addSubview:fromLabel];
    self.fromLabel = fromLabel;
//    fromLabel.backgroundColor = [UIColor orangeColor];
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.frame = CGRectMake(fromLabel.right, fromLabel.top, 30, 30);
    [shareButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [self.contentView addSubview:shareButton];
    [shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.shareButton = shareButton;
    
    self.percentWidth = titleLabel.width - emotionLabel.width - upLabel.width *2 - 50;
    
    [self.upLabel resetX:self.emotionLabel.right + 50 + 12];
    [self.upPercent resetX:self.emotionLabel.right + 50 + 12 + self.upLabel.width];
    
    UILabel *bottomLabel = [UILabel new];
    bottomLabel.frame = CGRectMake(0, fromLabel.bottom, kScreenWidth, 10);
    bottomLabel.backgroundColor = kColorRGB(240, 240, 240);
    [self.contentView addSubview:bottomLabel];
    self.bottomLabel = bottomLabel;
    
    
    //titleLabel.font.lineHeight = 16.71
    
}
- (void) shareButtonClick {
    
    
    
//    /tools/createShareImgByNewsId/{随机数}.jpeg?news_id=xxxxxx&base64=true
//    NSString *getImageWithNewsIdStrng = @"";
    
    NSString *getParameter = [NSString stringWithFormat:@"news_id=%@&base64=%@&platform=%@", self.shortInfoModel.ID, @"true", @"ios"];
    NSString *getUrl = [NSString stringWithFormat:@"%@%@", HTTPGetShareImageWithNewsID, getParameter];
    NSDictionary *getDic = @{};
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:kuserManager.lastVc.view animated:YES];
    hud.mode = MBProgressHUDModeAnnularDeterminate;
    hud.labelText = @"生成图片中···";
    
    self.shareButton.enabled = NO;
    [[DLNetWorkManager shareInstance] requestWithMethod:DLRequestMethodGET path:getUrl parameters:getDic success:^(id  _Nonnull responseObject) {
        [hud hide:YES];
        self.shareButton.enabled = YES;
        if (kRequestSuccess(responseObject)) {
            NSString *base64String = [NSString stringWithFormat:@"%@", responseObject[@"result"][@"base64Data"]];
            
            if ([base64String containsString:@"data:image/"]) {
                
                NSArray *arr = [base64String componentsSeparatedByString:@","];
                // 将base64字符串转为NSData
                NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:[arr lastObject] options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
                // 将NSData转为UIImage
                UIImage *decodeImage = [UIImage imageWithData:decodeData];
                
                [self showShareView];
                self.shareView.thirdShareType = DLShareType_newsFlashes;
                self.shareView.shareImage = decodeImage;
                
            } else {
                kShowRequestMsg(@"请求分享图片失败");
            }
        } else {
            kShowRequestMsg(kSafeString(responseObject[@"message"]));
            
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"请求接口失败，错误是：%@", error);
        [hud hide:YES];
        self.shareButton.enabled = YES;
    }];
}
- (void)showShareView {
    
    if (_shareView) {
        [_shareView showAnimation:YES];
    }else {
        _shareView = [DLShareView initWithPopViewWithSuperView:[UIApplication sharedApplication].keyWindow];
    }
}
- (void) setShortInfoModel:(DLInfoListModel *)shortInfoModel {
    _shortInfoModel = shortInfoModel;
    
    if (shortInfoModel.isNewsRead == YES) {
        self.titleLabel.textColor = kColorRGB(119, 119, 119);
    } else {
        self.titleLabel.textColor = kDLNormalBlackColor;
    }
    
    NSString *dateString = shortInfoModel.pubdate;
////    NSDate *date = [NSDate date];
//    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
//    dateFormatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";
////    NSString *dateStr = [dateFormatter stringFromDate:date];
//    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题
//    NSDate *dateFromString = [dateFormatter dateFromString:dateString];
    
    NSArray *dateArr = [dateString componentsSeparatedByString:@" "];
    NSString *hour = [dateArr lastObject];
    NSArray *secondArr = [hour componentsSeparatedByString:@":"];
    NSString *realTime = [NSString stringWithFormat:@"%@:%@", secondArr[0], secondArr[1]];
    
    NSString *realTitle = [NSString stringWithFormat:@"%@  %@", realTime, shortInfoModel.content];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:realTitle];
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:kDLBtnNormalBgColor
                    range:NSMakeRange(0, 5)];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 7.29;//调整行间距
    [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, realTitle.length)];
    
    self.titleLabel.attributedText = attrStr;
    
    self.fromLabel.text = [NSString stringWithFormat:@"%@", shortInfoModel.source];
    
    NSArray *percentArr = shortInfoModel.sentencevalue;
    
    NSString *upPercent = [percentArr firstObject];
    NSString *downPercent = [percentArr lastObject];
    
    self.upLabel.text = [NSString stringWithFormat:@"%.2f%%", upPercent.floatValue];
    self.downLabel.text = [NSString stringWithFormat:@"%.2f%%", downPercent.floatValue];
    
    CGFloat upWidth = self.percentWidth *(upPercent.doubleValue / (upPercent.doubleValue + downPercent.doubleValue));
    
    [self.upPercent resetWidth:upWidth];
    
    
    [self.downPercent resetX:self.upPercent.right - 12];
    [self.downPercent resetWidth:self.percentWidth - upWidth];
    
    
    CGFloat newHeight = [realTitle getValue4HeightWithWid:kScreenWidth - 40 andFont:kSystemFont(14)];
    
    
    
    newHeight = [realTitle attrGetValue4HeightWithWid:kScreenWidth - 40 andFont:kSystemFont(14) andLineSpace:paragraphStyle];
    
//    NSLog(@"高度是==%f", newHeight);
    if (shortInfoModel.isZhiXunShowAll == YES) {
        //展开状态
        paragraphStyle.lineSpacing = 7.29;
        [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, realTitle.length)];
        self.titleLabel.attributedText = attrStr;
        self.titleLabel.numberOfLines = 0;
        [self.titleLabel resetHeight:newHeight - 7.29 + 40];
    } else {
        //收起状态
//        newHeight == 23.997032
        
        if (newHeight > 65) {
//            NSLog(@"超过三行");
            self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
            self.titleLabel.numberOfLines = 3;
            [self.titleLabel resetHeight:64.7 - 7.29 + 40];
        } else {
//            NSLog(@"两行或三行");
            self.titleLabel.numberOfLines = 0;
            [self.titleLabel resetHeight:newHeight - 7.29 + 40];
        }
    }
    
    [self.emotionLabel resetY:self.titleLabel.bottom + 5];
    [self.upLabel resetY:self.emotionLabel.top];
    [self.upPercent resetY:self.upLabel.top + 4];
    [self.downPercent resetY:self.upPercent.top];
    [self.downLabel resetY:self.upLabel.top];
    [self.fromLabel resetY:self.emotionLabel.bottom + 5];
    [self.shareButton resetY:self.fromLabel.top];
    [self.bottomLabel resetY:self.fromLabel.bottom];
}

+ (CGFloat) getCellHeightWithModel:(DLInfoListModel *)model {
    CGFloat cellHeight = 0.0;
    
    NSString *dateString = model.pubdate;
    NSArray *dateArr = [dateString componentsSeparatedByString:@" "];
    NSString *hour = [dateArr lastObject];
    NSArray *secondArr = [hour componentsSeparatedByString:@":"];
    NSString *realTime = [NSString stringWithFormat:@"%@:%@", secondArr[0], secondArr[1]];
    
    NSString *realTitle = [NSString stringWithFormat:@"%@  %@", realTime, model.content];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 7.29;
    
    CGFloat newHeight = [realTitle attrGetValue4HeightWithWid:kScreenWidth - 40 andFont:kSystemFont(14) andLineSpace:paragraphStyle];
    
    if (model.isZhiXunShowAll == YES) {
        //展开
        cellHeight = 30 + 30 + 10 + newHeight - 7.29 + 40 ;
    } else {
        //收起
        if (newHeight > 65) {
            //超过三行
            cellHeight = 30 + 30 + 10 + 64.70 - 7.29 + 40;
        } else {
            cellHeight = 30 + 30 + 10 + newHeight - 7.29 + 40;
        }
    }
    
    return cellHeight;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
